package com.typy.www.tpay.ui.itemview;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.bean.vm.IcoTypeVm;
import com.typy.www.tpay.ui.acitivity.ActivicationActivity;
import com.typy.www.tpay.ui.acitivity.IcoBuyActivity;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.zhy.android.percent.support.PercentLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/28.
 */

public class IcoItemView {
    View mView;
    ViewHolder holder;
    private Context mContext;
    private SparseArray<CountDownTimer> countDownCounters;

    public IcoItemView(Context context) {
        mContext = context;
        mView = LayoutInflater.from(context).inflate(R.layout.item_ico_item, null);
        countDownCounters = new SparseArray<>();
        holder = new ViewHolder(mView);
    }

    public View getView() {
        return mView;
    }

    public void bind(final IcoTypeVm vm, final int position, final int classType) {
        if (vm != null) {
            String zonge = Double.valueOf(vm.getFdPrice()) * Double.valueOf(vm.getFdLimitCount()) +"";
            double progress = Double.valueOf(vm.getFdSellerCount()) * 100 / Double.valueOf(vm.getFdCount());
            Logutil.print("pro"+progress);
            final int shengyugesu = Integer.valueOf(vm.getFdCount()) - Integer.valueOf(vm.getFdSellerCount());

            UpdataUiUtil.getDefaul().setText("Zpay数字资产", holder.tvTypeName)
                    .setText("每个id限购" + vm.getFdLimitCount() + "枚", holder.tvLimit)
                    .setText("释放比例：100%", holder.tvBili)
                    .setText(vm.getFdCount(), holder.tvIcoguimo)
                    .setText("1Zpay=¥"+vm.getFdPrice(), holder.tvJiage)
                    .setText(vm.getFdStartTime(), holder.tvStarttime)
                    .setText(StringUtil.subZeroAndDot(progress)+ "%", holder.tvProgress);

            if (classType == ClassHoldUtil.ICOYURE) {
                holder.pbProgress.setProgress(0);

                UpdataUiUtil.getDefaul().setTime(countDownCounters, vm.getFdStartTime(), holder.tvShengyushijian).setText("剩余时间");
                holder.tvShengyushijian.setVisibility(View.VISIBLE);
                holder.tvShengyushijian2.setVisibility(View.GONE);


            } else if (classType == ClassHoldUtil.ICOJINXING) {
                if(progress==0){
                    holder.pbProgress.setProgress(0);
                }else if(progress<1&&progress>0){
                    holder.pbProgress.setProgress(1);
                }else{
                    holder.pbProgress.setProgress((int)progress);
                }

                UpdataUiUtil.getDefaul().setText(shengyugesu + "个", holder.tvShengyushijian2).setText("剩余个数");
                holder.tvShengyushijian.setVisibility(View.GONE);
                holder.tvShengyushijian2.setVisibility(View.VISIBLE);
            } else if (classType == ClassHoldUtil.ICOJIESU) {
                UpdataUiUtil.getDefaul().setText("已结束", holder.tvShengyushijian2).setText("剩余时间");
                holder.tvShengyushijian.setVisibility(View.GONE);
                holder.tvShengyushijian2.setVisibility(View.VISIBLE);
            }
                holder.llHolder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (classType == ClassHoldUtil.ICOJINXING){
                            ActivicationActivity.show(mContext,classType,vm.getFdPrice(),shengyugesu,vm.getFdId());
                        }
                    }
                });


        }

    }


    static class ViewHolder {
        @BindView(R.id.tv_type_name)
        TextView tvTypeName;
        @BindView(R.id.tv_limit)
        TextView tvLimit;
        @BindView(R.id.tv_bili)
        TextView tvBili;
        @BindView(R.id.tv_starttime)
        TextView tvStarttime;
        @BindView(R.id.tv_icoguimo)
        TextView tvIcoguimo;
        @BindView(R.id.tv_jiage)
        TextView tvJiage;
        @BindView(R.id.ll_amount)
        PercentLinearLayout llAmount;
        @BindView(R.id.tv_shengyutitle)
        TextView tvShengyutitle;
        @BindView(R.id.tv_shengyushijian)
        TextView tvShengyushijian;
        @BindView(R.id.tv_shengyushijian2)
        TextView tvShengyushijian2;
        @BindView(R.id.ll_point)
        PercentLinearLayout llPoint;
        @BindView(R.id.pb_progress)
        ProgressBar pbProgress;
        @BindView(R.id.tv_progress)
        TextView tvProgress;
        @BindView(R.id.ll_holder)
        PercentLinearLayout llHolder;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
